$buff_min = 2400; #40 minutes
$buff_max = 4800; #80 minutes
$countdown = 5; # Countdown time to MGB in minutes, default 5 minutes
$announce_min = 1200; #20 minutes
$announce_max = 2400; #40 minutes

@SpellList = ("Supernal Remedy","Daring","Bravery","Symbol of Ryltan","Symbol of Pinzarn","Armor of Protection","Temperance", "Blessing of Temperance","Aegolism","Virtue","Conviction","Hand of Conviction");
@SpellCost = ("1000","1000","2000","3000","4000","5000","10000","30000","15000","20000","30000","60000");
@SpellID = ("3465","89","244","486","487","1445","3692","4053","1447","3467","5257","5278");

sub EVENT_SPAWN {
   MASS_GROUP_BUFF();
   ANNOUNCE();
}

sub ANNOUNCE {
   $announce_timer = int(rand($announce_max)) + $announce_min;
   quest::settimer("announce_senara", $announce_timer); # Random announcements
}

sub MASS_GROUP_BUFF {
   $mgb_timer = int(rand($buff_max)) + $buff_min;
   quest::settimer("mgb_senara", $mgb_timer);
   $minutes = $countdown;
}

sub EVENT_TIMER {
   if($timer eq "mgb_senara") {
      quest::shout("MGB Virtue at main bank, come one, come all!!");
      quest::stoptimer("mgb_senara");
      quest::stoptimer("announce_senara");
      quest::settimer("countdown_senara", 60); # 60 second count before each announcement
   }
     
   if($timer eq "countdown_senara") {
      if($minutes > 0) {
         quest::shout("Casting MGB Virtue in " . $minutes . " minutes.");
         $minutes--;
      } else {
         quest::shout("Casting Virtue now! Stick close to me!");
         quest::stoptimer("countdown_senara");
         $npc->SetAppearance(0);
         my @moblist = $entity_list->GetMobList();
         foreach $ent (@moblist) {
            my $dist = $ent->CalculateDistance($npc->GetSpawnPointX(),$npc->GetSpawnPointY(),$npc->GetSpawnPointZ());
            my $level = $ent->GetLevel();
            if(int($dist)<100 && $ent->IsClient())
				{
					$ent->CastSpell(3479, $ent, 10, -1, -1); #I cast virtue on myself
				}
	  }		
      ANNOUNCE();
      MASS_GROUP_BUFF();
      }
   }

   if($timer eq "announce_senara") {
      my $random_number = int(rand(5));
      if ($random_number == 0)
         {
         quest::shout("Having a hard time? For donations i will bless you! Come behind the main bank.");
         }
      elsif ($random_number == 1)
         {
         quest::shout("Before Aegolism was Heroism, become a Hero with my blessings behind the main bank!");
         }
      elsif ($random_number == 2)
         {
         quest::shout("Blessings for Donations! Donations go to the Cult of Mithaniel Marr.");
         }
      elsif ($random_number == 3)
         {
         quest::shout("Have a easier day with my blessings behind the main bank!");
         }
      elsif ($random_number == 4)
         {
         quest::shout("My blessings make even a wimp look strong! Come see me behind the main bank.");
         }
      $npc->SetAppearance(int(rand(2)));
      ANNOUNCE();
    }

   if($timer eq "despawn") {
      quest::say ("I'm off to shower. This place makes my skin crawl.");
      quest::depop();
   }
}

sub EVENT_SAY
{
   my $NPCName = $npc->GetCleanName();
   $client->Message(315, " ");
   if ($text=~/hail/i) {
      my $sl1 = quest::saylink("blessings", 1);
      $client->Message(315, "$NPCName says, 'Greetings, $name. Have you come to recieve my $sl1?'");
      plugin::DoAnim(bowto);
   } elsif ($text=~/blessings/i) {
      my $n = 0;
      $client->Message(315, "$NPCName says, 'Please, tell me what blessing you wish to recieve $name.'");
      $client->Message(12, "Warning! By clicking on the name of a Buff, you will be charged automatically for the asked amount.");
      while ($SpellList[$n]) {
            my $SpellList = quest::saylink($n, 1, $SpellList[$n]);
            my $SpellCost = $SpellCost[$n];
            $client->Message(315, "- $SpellList for ".($SpellCost/1000)." Platinum");
         $n++;
      }
   } else {
      if (($text =~ m/\d/) && $SpellList[$text]) {
         my $PCMoney = $client->GetCarriedMoney();
         if ($PCMoney >= $SpellCost[$text]) {
            $client->Message(315, "$NPCName says, 'Thank you $name! I shall cast $SpellList[$text] for you. Good luck out there!");
            plugin::DoAnim(cast);
            quest::selfcast($SpellID[$text]);
            $client->TakeMoneyFromPP($SpellCost[$text], true);
         } else {
            $client->Message(315, "$NPCName says, 'I am sorry $name, you don't have enough Money with you.");
         }
      }
   }
}

sub EVENT_ITEM {
   my $NPCName = $npc->GetCleanName();
   $client->Message(315, "$NPCName says, 'Thank you, but you should hold on to it.'");
   plugin::return_items(\%itemcount);
   if ($platinum != 0 || $gold !=0 || $silver != 0 || $copper != 0) {
      quest::givecash($copper, $silver, $gold, $platinum);
   }
}